<template>
  <el-card class="box-card">
    <el-card class="box-form">
      <template #header>
        <div class="card-header">
          <span>微商城管理系统</span>
        </div>
      </template>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600ox"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" type="text" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
          <el-button type="primary" @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-card>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import useToken from "../stores/token";
import notification from "../utils/notification";

// import {ref,reactive} from 'vue'
// import {useRouter} from 'vue-router'
// import useToken from '../stores/token'
// import notification from '../utils/notification';
import { login } from "../api/api";

// const ruleFormRef=ref()
// const ruleForm=reactive({
//     password:'',
//     username:''
// })

// const rules=reactive({
//     username:[{required:true,message:"请输入用户名",trigger:'blur'},{min:3,max:10,message:"用户名长度在3-10之间",trigger:'blur'}],
//     password:[{required:true,message:"请输入密码",trigger:'blur'},{min:5,max:10,message:"密码长度在5-10之间",trigger:'blur'}]

// })

// const router=useRouter()
// const {updateToken}=useToken()
// const submitForm=ruleFormRef=>{
//     ruleFormRef.validate(async valid=>{
//         if(valid){
//             const data=await login(ruleForm)
//             if(data){
//                 updateToken(data.token)
//                 router.push({name:"index"})
//             }

//         }
//         else{
//             notification({
//                 message:"用户名或密码错误",
//                 type:"error"
//             })
//         }
//     })
// }

// const resetForm=()=>{
//     ruleFormRef.value.resetFields()
// }

const ruleForm = reactive({
  password: "",
  username: "",
});

const ruleFormRef = ref();

const rules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "用户名长度在3-10之间", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 5, max: 10, message: "请输入长度在5-10之间", trigger: "blur" },
  ],
});

const router = useRouter();
const { updateToken } = useToken();

const submitForm = (ruleFormRef) => {
  ruleFormRef.validate(async (valid) => {
    if (valid) {
      const data = await login(ruleForm);
      if (data) {
        updateToken(data.token);
        router.push({ name: "index" });
      }
    } else {
      notification({
        type: "error",
        message: "用户名或密码错误",
      });
    }
  });
};

const resetForm = () => {
  ruleFormRef.value.resetFields();
};

// import {useRouter} from 'vue-router'
// import notification from '../utils/notification';
// import {ref,reactive} from 'vue'
// import useToken from '../stores/token'

// import {login} from '../api/api';
//  const ruleFormRef = ref()
//  const ruleForm = reactive({
//      username: "",
//      password: ""

//  })
//  const rules = reactive({
//      username: [{
//              required: true,
//              message: "请输入用户名",
//              trigger: 'blur'
//          },
//          {
//              min :3,
//              max : 10,
//              message: "用户长度在3-10之间",
//              trigger: "blur"
//          }
//      ],
//      password: [{
//          required: true,
//          message: "请输入密码",
//          trigger: "blur"
//      }, {
//          min: 5,
//          max: 10.,
//          message: "密码长度在5-10之间",
//          trigger: "blur"
//      }]

//  })
// const router = useRouter()
// const {updateToken}=useToken();
//  const submitForm = formEl => {
//     formEl.validate(async valid =>{
//         if(valid){
//             const data =await login (ruleForm);
//             if(data){
//                 updateToken(data.token)
//                 router.push({name:'index'})
//             }
//         }else{
//             notification({
//                 message:"用户名或者密码错误",
//                 type:"error"
//             })
//         }
//     })

//  }
//  const resetForm = () => {
//      ruleFormRef.value.resetFields()
//  }
</script>
<style lang="scss" scoped>
.box-card {
  height: 100%;
  background: rgba(38, 72, 176) url("/images/loginBg.jpg") center / cover no-repeat;

  .box-form {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 70%;
    max-width: 750px;
    transform: translate(-50%, -50%);

    .card-header {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .el-form {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;

      .el-form-item {
        width: 70%;
        display: flex;
        align-items: center;
        justify-content: center;
        --el-form-label-font-size: 16px;
        margin-top: 15px;
        margin-bottom: 15px;

        .button {
          width: 90px;
        }

        &.center {
          display: flex;
          justify-content: center;
        }
      }
    }
  }
}
</style>
